package com.glsc.ngateway.opmanage.controller.deposit;

import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.User;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.deposit.DepositoryPaymentPlan;
import com.glsc.ngateway.opmanage.aspect.UserCache;
import com.glsc.ngateway.opmanage.dto.DepositoryPaymentDto;
import com.glsc.ngateway.opmanage.service.deposit.DepositoryPaymentPlanService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;


/**
 * 保证金划款方案
 */
@RestController
@RequestMapping("/api/depository/paymentPlan")
@Slf4j
public class DepositoryPaymentPlanController {

    @Resource
    private DepositoryPaymentPlanService depositoryPaymentPlanService;

    @Resource
    private UserCache userCache;

    @GetMapping("/createPlan")
    public PlatformResponse<?> createPlan(HttpServletRequest request) {
        User user = userCache.get(request);
        depositoryPaymentPlanService.create(user.getId(), user.getName());
        return PlatformResponse.success();
    }

    /**
     * 发送新意支付指令
     *
     * @param request
     * @return
     */
    @PostMapping("/sendXinYi")
    public PlatformResponse<?> sendXinYi(HttpServletRequest request, @RequestBody DepositoryPaymentDto dto) {
        User user = userCache.get(request);
        if (CollectionUtils.isEmpty(dto.getIds())) {
            return PlatformResponse.failedMsg("请选择要提交的新意指令");
        }
        depositoryPaymentPlanService.send(user, dto.getIds());
        return PlatformResponse.success();
    }

    /**
     * 分页查询划款方案
     *
     * @param dto
     * @param request
     * @return
     */
    @PostMapping("/page")
    public PlatformResponse<Page<DepositoryPaymentPlan>> pageTemplate(@RequestBody DepositoryPaymentDto dto, HttpServletRequest request) {

        Map<String, Object> param = new HashMap<>();
        if (StringUtils.isNotBlank(dto.getName())) {
            param.put("LIKE_name", dto.getName());
        }
        if (StringUtils.isNotBlank(dto.getEffectDate())) {
            param.put("LIKE_effectDate", dto.getEffectDate());
        }
        Page<DepositoryPaymentPlan> page = depositoryPaymentPlanService.page(dto.getPageNo(), dto.getPageSize(), param);

        return PlatformResponse.successData(page);
    }

    /**
     * 保存
     *
     * @param obj
     * @param request
     * @return
     */
    @PostMapping("/save")
    public PlatformResponse<DepositoryPaymentPlan> save(@RequestBody DepositoryPaymentPlan obj, HttpServletRequest request) {

        DepositoryPaymentPlan plan = depositoryPaymentPlanService.save(obj);
        return PlatformResponse.successData(plan);
    }

    /**
     * 根据id删除
     *
     * @param obj
     * @param request
     * @return
     */
    @PostMapping("/delete")
    public PlatformResponse<?> delete(@RequestBody DepositoryPaymentPlan obj, HttpServletRequest request) {

        depositoryPaymentPlanService.deleteAllById(obj.getId());
        return PlatformResponse.success();
    }


}
